﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using FinPlusAssembler;
using FinPlusInterfaces;

namespace FinPlusCompCore
{
    public class FinPlusConnectService : FinPlusComponent
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        [State(Test = true)]
        public override string TestString { get { return Adapter.ToString(); } }
        public override IPods Adapter { get { return _conn.GetPods(_serviceName); } }

        private enum Params { Env, Group, Query };
        private readonly IServiceConn _conn;
        private readonly bool _transpose = false;
        private readonly string _env, _group, _serviceName, _serviceKey, _keyFormat, _query;

        //construct
        public FinPlusConnectService(string serviceName, string serviceType, string query, string keyFormat)
        {
            _conn =  IOC.New<IServiceConn>();
            _serviceKey = _conn.Key(_serviceName = serviceName, _query = query, _keyFormat = keyFormat);
            Config = serviceName;

            if (!query.RegxTryGet(@"\[*group\]*,(?<result>.*?)\;", out _group))
                throw new ServiceConnException(string.Format("Service type {0} name {1} cant extract group", serviceType, _serviceName));

            if (!query.RegxTryGet(@"\[*env\]*,(?<result>.*?)\;", out _env))
                throw new ServiceConnException(string.Format("Service type {0} name {1} cant extract env", serviceType, _serviceName));

            Env = _env.EnumParse<Env>(Env.unknown);
            _conn.Connect(Env, Config, User, _serviceKey, serviceType, _query.TrimEnd(';'), _transpose, false);

            IsValid = true;
        }

        //common control interface 
        public override void Dispose()
        {
            if(_conn != null)
                _conn.Dispose(_serviceKey);
        }
    }
}


























































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus